<template>
  <el-drawer
    :visible.sync="drawer"
    direction="rtl"
    size="50%"
    :with-header="false"
    :width="700"
  >
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img
              class="drawer_t_l_img"
              src="/static/skin/herodoo/main/platform/home/img/piliangoumai.png"
              alt=""
            />
            创建打卡点
          </div>
          <img
            class="drawer_t_r"
            src="/static/skin/herodoo/main/platform/home/img/guanbi.png"
            alt=""
            @click="drawer = false"
          />
        </div>
      </div>
      <div class="drawer_content">
        <div class="list-box">
          <div class="left">打卡点名称：</div>
          <div class="right">
            <el-input
              v-model="form.title"
              placeholder="请输入打卡点名称"
            ></el-input>
          </div>
        </div>
        <div class="list-box">
          <div class="left">打卡点图片:</div>
          <div class="right">
            <div class="tips">
              可上传4张图片，图片不超过512KB，仅支持jpg、png格式,第一张图片将作为主图展示，建议比例1:1
            </div>
            <imgUplode
              :list="form.fileList"
              @imgList="getImgsUrl"
              :octopus="octopus"
              num="4"
              multiple
            >
            </imgUplode>
          </div>
        </div>
        <div class="list-box">
          <div class="left">打卡点经纬度：</div>
          <div class="right">
            <div class="row">
              <span>打卡点经度：</span>
              <el-input
                v-model="form.lng"
                placeholder="请输入打卡点经度"
              ></el-input>
            </div>
            <div class="row">
              <span>打卡点纬度：</span>
              <el-input
                v-model="form.lat"
                placeholder="请输入打卡点纬度"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="list-box">
          <div class="left">打卡点地址：</div>
          <div class="right">
            <el-input
              v-model="form.addressName"
              placeholder="请输入打卡点地址"
            ></el-input>
          </div>
        </div>
        <!-- <Bmap @getPoint="getPoint"></Bmap> -->
        <div class="list-box">
          <div class="left">打卡范围：</div>
          <div class="right">
            进入打卡点直径
            <el-input
              class="small"
              v-model="form.max"
              placeholder="请输入打卡直径"
            ></el-input>
            米内，可打卡
          </div>
        </div>

        <!-- <div class="list-box">
          <div class="left">打卡方式:</div>
          <div class="right">
            <el-select
              v-model="form.typeid"
              clearable
              placeholder="请选择打卡方式"
            >
              <el-option
                v-for="item in wayList"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </div>
        </div> -->

        <div class="list-box">
          <div class="left">点位详情：</div>
          <div class="right">
            <quill
              ref="quill"
              v-if="drawer"
              :octopus="octopus"
              :desc="form.deltedContent"
              @quillContent="getQuillContent"
            ></quill>
          </div>
        </div>
      </div>

      <div class="button_list">
        <button @click="save()">保存</button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import imgUplode from "@/components/Components/imgUpdate.vue";
import quill from "@/components/Components/quillEditor.vue";
// import Bmap from "./BMap.vue"
export default {
  name: "cat_dialog",
  components: {
    imgUplode,
    quill
    // Bmap
  },
  props: {
    // isEdit: {
    //   type: Boolean,
    //   default: true
    // }
  },
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      isEdit: "look",
      wayList: [],
      form: {
        title: "",
        fileList: [],
        addressName: "",
        lat: "",
        lng: "",
        max: "",
        deltedContent: "",
        typeid: ""
      },
      rules: {
        title: { required: true, message: "请输入打卡点名称" },
        fileList: { required: true, message: "至少上传1张打卡点图片" },
        lng: { required: true, message: "请输入经度" },
        lat: { required: true, message: "请输入纬度" },
        addressName: { required: true, message: "请输入打卡点地址" },
        max: {
          required: true,
          message: "请输入打卡范围",
          reg: () => {
            return this.max > 0 ? false : true;
          }
        },
        deltedContent: { required: true, message: "请输入点位详情" },
        typeid: { required: false, message: "请选择打卡方式" }
      },
      map: null
    };
  },
  created() {
    this.getWay_1298();
  },
  watch: {},
  methods: {
    drawerOpen(data = null, type = "CREATE") {
      // editType 是否可编辑，打他 父级传的数据，type  是新建还是更新
      this.drawer = true;
      this.isEdit = type;
      if (!data) {
        // for (let [k, v] of Object.entries(this.form)) {
        //   console.log(typeof v,v,k);
        //   typeof v == "string" ? (this.form[k] = "") : (this.form[k] = []);
        // }
        return;
      } else {
        (this.form.title = data.Name),
          (this.form.fileList = data.ImageUrl != null
            ? JSON.parse(data.ImageUrl)
            : []);
        this.form.addressName = data.Address;
        this.form.lat = data.YAxis;
        this.form.lng = data.XAxis;
        this.form.max = data.Scope;
        this.form.deltedContent = data.Description;
        // this.form.typeid = data.TypeID
        this.form.ID = data.ID;
      }
    },
    getQuillContent(val) {
      this.form.deltedContent = val;
    },
    getImgsUrl(val) {
      this.form.fileList = val;
    },
    save() {
      if (!this.octopus.tools.verify(this.form, this.rules)) {
        return;
      }
      this.$confirm(`确定保存这个打卡点吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: 'warning'
        center: true
      })
        .then(res => {
          this.update_api_1290();
        })
        .catch(() => {
          console.log("取消了");
        });
    },
    update_api_1290() {
      let cellDate = [
        { name: "Address", defaultValue: this.form.addressName }, // Address
        { name: "Description", defaultValue: this.form.deltedContent }, // Description
        { name: "Name", defaultValue: this.form.title }, // Name
        { name: "Scope", defaultValue: this.form.max }, // Range
        { name: "Url", defaultValue: JSON.stringify(this.form.fileList) }, // Url
        { name: "XAxis", defaultValue: this.form.lng }, // XAxis
        { name: "YAxis", defaultValue: this.form.lat } // YAxis
        // { name: "TypeID", defaultValue: this.form.typeid }
      ];
      if (this.isEdit == "CREATE") {
        cellDate.push({
          name: "CreateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "CreateUserID",
          defaultValue: this.octopus.passport.id
        });
      } else {
        cellDate.push({
          name: "UpdateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "UpdateUserID",
          defaultValue: this.octopus.passport.id
        });
      }
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1290,
          type: this.isEdit,
          value: this.isEdit == "CREATE" ? "" : this.form.ID,
          cellData: cellDate,
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.drawer = false;
            for (let [k, v] of Object.entries(this.form)) {
              // console.log(typeof v, v, k);
              typeof v == "string" ? (this.form[k] = "") : (this.form[k] = []);
            }
            this.octopus.tips.success("操作成功。");
            this.$emit("reload");
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // search() {
    //   console.log(this.form.addressName, this.octopus);
    //   //   this.octopus.tools.openAPI
    //   this.octopus.request.api.post(
    //     "/ESB/Services/Tools/Data/Get",
    //     {
    //       requestType: "POST_JSON", // POST_JSON
    //       //   url: "https://uri.amap.com/search?keyword=美食&center=121.515601,31.233456&city=310000&view=map&src=mypage&coordinate=gaode&callnative=0",
    //       url:
    //         "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/PT202405210111200031?mchid=1650102004",
    //       params: {}
    //     },
    //     apiData => {
    //       console.log(apiData);
    //     }
    //   );
    // },
    getPoint(e) {
      this.form.lng = e.lng;
      this.form.lat = e.lat;
    },
    getWay_1298() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1298,
          cellData: [
            { name: "ID" }, // ID
            { name: "Name" }, // Name
            { name: "ParentkeyValue" } // ParentkeyValue
          ],
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.wayList = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
  font-size: 1.6rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_content .driver-title {
  margin-top: 1em;
}

.content-box {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}

.mt-10 {
  margin-top: 10rem;
}

.content-box .label {
  width: 25%;
  font-size: 1.6rem;
}

.content-box .label .label-bold {
  font-size: 1.8rem;
  font-weight: bold;
}

.content-box .desc {
  flex: 1;
  font-size: 1.6rem;
}

.list-box {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.list-box {
  line-height: 4rem;
  margin-top: 2rem;
}

.list-box .left {
  width: 20%;
  font-size: 1.5rem;
}

.list-box .right {
  width: 80%;
}

.button_list {
  margin-top: 1rem;
  display: flex;
  justify-content: end;
  position: absolute;
  background-color: #ffff;
  bottom: 1rem;
  width: calc(100% - 7rem);
}

.button_list button {
  border: none;
  height: 4rem;
  width: 12rem;
  border-radius: 1rem;
  margin-left: 1rem;
  color: #ffff;
  background-color: #f78c53;
  cursor: pointer;
}
.row {
  display: flex;
  flex-direction: row;
  margin: 20px 0;
}
.row span {
  width: 5vw;
}
.small {
  width: 10vw;
  padding: 0 20px;
}
</style>
